//
//  AFDownManager.m
//  Fund
//
//  Created by Hepburn on 15/7/28.
//  Copyright (c) 2015年 miracle. All rights reserved.
//

#import "AFDownManager.h"
#import "AFNetworking.h"
#import <CommonCrypto/CommonDigest.h>
#import "AppDelegate.h"
@implementation AFDownManager

+ (void)GET:(NSString *)URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.allowInvalidCertificates = YES;
    mgr.securityPolicy = securityPolicy;
    
    [mgr GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //  请求成功的回调
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //  请求失败的回调
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)POST:(NSString *)URLString parameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure {
    
    
     NSLog(@"%@?%@", URLString, [AFDownManager appendParameters:parameters]);
    
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.allowInvalidCertificates = YES;
    mgr.securityPolicy = securityPolicy;
    
    [mgr POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
    
}
+(NSString *)appendParameters:(id)parameters
{
    NSDictionary *dict = (NSDictionary *)parameters;
    NSArray *allKeys = dict.allKeys;
    NSString *string = @"";
    for (NSString *key in allKeys) {
        string = [string stringByAppendingFormat:@"%@%@=%@", string.length==0?@"":@"&", key,dict[key]];
    }
    
    return string;
}


+ (NSMutableDictionary *)setDictionary:(NSMutableDictionary *)dict{
//    [dict setObject:[AFDownManager GetSignString:dict] forKey:@"signmsg"];
//    [dict setObject:[AFDownManager MD5String:str ] forKey:@""]
//    NSLog(@"%@", dict);
    return dict;
}
+ (NSString *)MD5String:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}
+ (NSString *)GetUserID{
    
    NSUserDefaults *usdf=[NSUserDefaults standardUserDefaults];
    NSString *value1 = [usdf objectForKey:@"userID"];
    
    return value1;
    
}

//+ (NSString *)GetDateTimeString {
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    formatter.dateFormat = @"yyyy-MM-dd HH:mm:SS";
//    NSLog(@"%@",formatter.dateFormat);
//    NSString *datestr = [formatter stringFromDate:[NSDate date]];
//    datestr = [datestr stringByReplacingOccurrencesOfString:@":" withString:@""];
//    datestr = [datestr stringByReplacingOccurrencesOfString:@" " withString:@""];
//    datestr = [datestr stringByReplacingOccurrencesOfString:@"-" withString:@""];
//    NSLog(@"----%@",datestr);
//    
//    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
//    NSTimeInterval a =[dat timeIntervalSince1970]*1000;
//    NSString *timeString = [NSString stringWithFormat:@"%f", a];
//    NSLog(@"====%@",dat);
//    return datestr;
//}

//取系统时间
//+ (NSString *)GetDateString {
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    formatter.dateFormat = @"yyyy-MM-dd";
//    NSLog(@"%@",formatter.dateFormat);
//    NSString *datestr = [formatter stringFromDate:[NSDate date]];
//    return datestr;
//}

//+ (NSString *)GetSignString:(NSDictionary *)dict {
//    NSString *signStr = KKMerPass;
//    NSArray *keys = [dict.allKeys sortedArrayUsingSelector:@selector(compare:)];
//    for (NSString *key in keys) {
//        signStr = [signStr stringByAppendingFormat:@"%@%@", key, dict[key]];
//    }
//    signStr = [signStr stringByAppendingString:KKMerPass];
//    NSLog(@"%@", signStr);
//    return [AFDownManager MD5String:signStr];
//}



@end
